var express = require('express');
var router = express.Router();
var db = require("../sqlcon");
/* GET home page. */

router.get('/user_add', function(req, res, next) {
    var sql = "select * from groupes";
    db.query(sql,function (err,data) {
        var group_data = data;
        var sql = "select * from department";
        db.query(sql,function (err,data) {
            var department_data = data;
            res.render('user_add',{groupList:group_data,departList:department_data});
        });
    });
});
router.get('/user_edit', function(req, res, next) {
    res.render('user_edit' );
});
router.get('/user_list', function(req, res, next) {
    var sql = "select * from loginuser";
    db.query(sql,function(err,data) {
        if (err) {
            console.log(err);
        }
        else {
            res.render("user_list", {userlist: data});
        }
    })
});
router.post("/insertuser",function(req,res,next){
    var userName = req.body.username;
    var userCode = req.body.usercode;
    var userGroup = req.body.usergroup;
    var userDepart = req.body.userdepart;
    var pwd = req.body.pwd;
    var description = req.body.description;
    var createTime =new Date().getFullYear() + "," + new Date().getMonth() + "," +  new Date().getDate();
    var sql = "insert into loginuser (userName,userCode,userGroup,userDepart,pwd,description,createTime) values (?,?,?,?,?,?,?)";
    db.query(sql,[userName,userCode,userGroup,userDepart,pwd,description,createTime],function(err,data){
        if(err){
            console.log(err)
        }
        else{
            res.redirect("/user_add");
        }
    })
});
router.post("/deuser",function (req,res,next) {
    var userId = req.body.userId;
    var sql="delete from loginuser where userId=?";
    db.query(sql,[userId],function (err, data) {
        if (err) {
            console.log(err);
        }
        else{
            res.send("删除成功");
        }
    })
});
router.post("/edituser",function (req,res,next) {
    var userName = req.body.username;
    console.log(userName);
    var pwd = req.body.pwd;
    console.log(pwd);
    var description = req.body.description;
    console.log(description);

    var userCode = req.body.usercode;
    console.log(userCode);
    var userId = req.body.userId;
    console.log(userId);
    var sql="update loginuser set userName=?,pwd=?,description=?,userCode=? where userId=?";
    db.query(sql,[userName,pwd,description,userCode,userId],function(err,data){
        console.log(data);
        if(err){
            console.log(err)
        }
        else if (data.affectedRows == 1){
            res.redirect("/user_list");
        }
        else{
            res.send("添加机构异常");
        }
    })
});
router.post("/searchuser",function(req,res,next){
    var userName = req.body.searchcontent;
    var sql = "select * from loginuser where userName like '%"+userName+"%'";
    db.query(sql,function(err,data){
        if(err){
            res.send("数据库操作异常");
        }
        else if(data.length > 0){
            res.render("user_list",{userlist:data});
        }
        else{
            res.send("用户名不正确");
        }
    })
});
router.post("/delete",function (req,res,next) {
    var choosestr = req.body.choosee;
    var choosees=choosestr.split(",");
    var chooselist="?";
    for(var i=0;i<choosees.length-1;i++){
        chooselist +=",?";
    }
    var sql="delete from loginuser where userId in ("+chooselist+")";
    db.query(sql,choosees,function (err, data) {
        if (err) {
            console.log(err);
        }
        else if (data.affectedRows==choosees.length) {
            res.send("删除成功");
        }
        else{
            res.send("删除异常");
        }
    })
});
module.exports = router;
